﻿using System.Xml;
using System.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Marbles.Core.Logging
{
    public sealed class XmlFileLogger : LoggerBase, ILogger
    {

        public string LogFileDirectory
        {
            get
            {
                string value = System.Configuration.ConfigurationManager.AppSettings["LogFileDirectory"];
                if (value == null) { value = string.Empty; }
                return value;
            }
        }

        public string LogFileDateFormat
        {
            get
            {
                string value = System.Configuration.ConfigurationManager.AppSettings["LogFileDateFormat"];
                if (value == null) { value = "yyyyMMdd"; }
                return value;
            }
        }

        public void Log(string Source, string Title, string Message, EntryType Type)
        {
            Log(Source, Title, Message, Type, DateTime.Now, 0);
        }

        public void Log(string Source, string Title, string Message, EntryType Type, DateTime TimeStamp)
        {
            Log(Source, Title, Message, Type, TimeStamp, 0);
        }

        public void Log(string Source, string Title, string Message, EntryType Type, int EventID)
        {
            Log(Source, Title, Message, Type, DateTime.Now, EventID);
        }

        public void Log(string Source, string Title, string Message, EntryType Type, DateTime TimeStamp, int EventID)
        {
            LogEntry entry = new LogEntry();
            entry.Source = Source;
            entry.Title = Title;
            entry.Message = Message;
            entry.Type = Type;
            entry.TimeStamp = TimeStamp;
            entry.EventID = EventID;
            Log(entry);
        }

        public void Log(LogEntry Entry)
        {
            if (!IsLoggingEnabled()) { return; }

            string filename = Path.Combine(LogFileDirectory, string.Format("{0}.xml", DateTime.Now.ToString(LogFileDateFormat)));

            if (!File.Exists(filename))
            {
                StreamWriter sw = new StreamWriter(filename, true);
                XmlTextWriter tw = new XmlTextWriter(sw);
                tw.WriteStartDocument(true);
                tw.WriteStartElement("LogEntries");
                tw.WriteEndElement();
                tw.Flush();
                tw.Close();
            }

            XmlDocument xdoc = new XmlDocument();
            xdoc.Load(filename);
            XmlNodeList rootnodes = xdoc.GetElementsByTagName("LogEntries");

            if (rootnodes.Count > 0)
            {
                XmlNode root = rootnodes[0];
                XmlNode node = xdoc.CreateElement("LogEntry");
                node.Attributes.Append(CreateAttribute(xdoc, "Source", Entry.Source));
                node.Attributes.Append(CreateAttribute(xdoc, "Title", Entry.Title));
                node.Attributes.Append(CreateAttribute(xdoc, "Message", Entry.Message));
                node.Attributes.Append(CreateAttribute(xdoc, "Type", Entry.Type.ToString()));
                node.Attributes.Append(CreateAttribute(xdoc, "TimeStamp", Entry.TimeStamp.ToString("dd-MM-yyyy HH:mm:ss")));
                node.Attributes.Append(CreateAttribute(xdoc, "EventId", Entry.EventID.ToString()));
                root.AppendChild(node);
                xdoc.Save(filename);
            }

        }

        private XmlAttribute CreateAttribute(XmlDocument xdoc, string name, string value)
        {
            XmlAttribute attr = xdoc.CreateAttribute(name);
            attr.Value = value.Trim();
            return attr;
        }
    }
}
